home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™94 / Talks & Papers / Michael D. Crawford↵ / Word Services SDK 1.0.5 / Writeswell Jr. Source / AEObj.c next >
Text File  |  1992-09-24  |  5KB  |  195 lines

  1. /* AEObj.c
  2.  * Utilities for dealing with Apple Event Objects (making descriptors, etc.)
  3.  * ©1992 Working Software, Inc.
  4.  * This source code is copyrighted.  Permission is granted to use the Word Services
  5.  * portion of the Writeswell Jr. source code in your own programs, but you 
  6.  * may not distribute the Writeswell Jr. word-processor code as a 
  7.  * commercial product.  If you modify the code, please do not call it 
  8.  * Writeswell Jr. (or Writeswell.)  This will ensure that people understand the 
  9.  * program and don’t have to deal with a number of different versions with 
  10.  * who-knows-what going on in the code.
  11.  * 
  12.  * Writeswell Jr. and Writeswell are trademarks of Working Software, Inc.
  13.  * 18 Dec 91 Mike Crawford
  14.  */
  15.  
  16. #include <AppleEvents.h>
  17. #include <AEObjects.h>
  18. #include <AERegistry.h>
  19. #include "TBConstants.h"
  20. #include "TBGlobals.h"
  21. #include "AEObj.h"
  22. #include "ObWind.h"
  23. #include "ObText.h"
  24. #include "ObOspec.h"
  25. #include "Gripe.h"
  26.  
  27. OSErr InstallMyAccessors( void );
  28. pascal OSErr MyCountProc(DescType desiredType,
  29.                         DescType containerClass,
  30.                         const AEDesc *container,
  31.                         long *result);
  32. pascal OSErr MyDisposeTokenProc(AEDesc *unneededToken);
  33. OSErr InstallMyCoercers( void );
  34.  
  35. OSErr InitAEObjStuff( void )
  36. {
  37.     OSErr err;
  38.     
  39.     err = AEObjectInit();
  40.     if ( err ){
  41.         Gripe( "\pAEObjectInit failed" );
  42.         return err;
  43.     }
  44.     
  45.     err = InstallMyAccessors();
  46.     if ( err ){
  47.         Gripe( "\pInstallMyAccessors failed" );
  48.         return err;
  49.     }
  50.  
  51.     /* Install the callback functions used by the AEObj parser */
  52.     err = AESetObjectCallbacks((compareProcPtr)nil, 
  53.             (countProcPtr)MyCountProc, 
  54.             (disposeTokenProcPtr)MyDisposeTokenProc,
  55.             (getMarkTokenProcPtr)nil,
  56.             (markProcPtr)nil, 
  57.             (adjustMarksProcPtr)nil, 
  58.             (getErrDescProcPtr)nil);
  59.     
  60.     err = InstallMyCoercers();
  61.     if ( err ){
  62.         Gripe( "\pInstallMyCoercers failed" );
  63.         return err;
  64.     }
  65.  
  66.     return noErr;
  67. }
  68.  
  69. OSErr InstallMyAccessors( void )
  70. {
  71.     OSErr err;
  72.     
  73.     /* We install the accessors to get various objects from various containers */
  74.     
  75.     /* Get a Window element from an application (null) container */
  76.     err = AEInstallObjectAccessor(cWindow,
  77.                                 typeNull,
  78.                                 (accessorProcPtr)WindFromNull,
  79.                                 0,
  80.                                 false);
  81.  
  82.     /* Get any property from a window container */
  83.     err = AEInstallObjectAccessor( typeProperty,
  84.                                 cWindow,
  85.                                 (accessorProcPtr)PropFromWind,
  86.                                 0,
  87.                                 false);
  88.  
  89.     /* Get text from a window container */
  90.     err = AEInstallObjectAccessor( cText,
  91.                                 cWindow,
  92.                                 (accessorProcPtr)TextFromWind,
  93.                                 0,
  94.                                 false);
  95.  
  96.     /* Get a word from a TextEdit text container */
  97.     err = AEInstallObjectAccessor( cWord,
  98.                                 typeTEText,
  99.                                 (accessorProcPtr)WordFromTEText,
  100.                                 0,
  101.                                 false);
  102.  
  103.     /* Get a char (or set thereof) from a TextEdit text container */
  104.     err = AEInstallObjectAccessor( cChar,
  105.                                 typeTEText,
  106.                                 (accessorProcPtr)CharFromTEText,
  107.                                 0,
  108.                                 false);
  109.  
  110.     /* Get any property from a text container */
  111.     err = AEInstallObjectAccessor( typeProperty,
  112.                                 typeTEText,
  113.                                 (accessorProcPtr)PropFromTEText,
  114.                                 0,
  115.                                 false);
  116.  
  117.     /* Get an object specifier from a window container (for Word Services) */
  118.     err = AEInstallObjectAccessor( typeObjectSpecifier,
  119.                                 cWindow,
  120.                                 (accessorProcPtr)OspecFromWind,
  121.                                 0,
  122.                                 false);
  123.  
  124.     return noErr;
  125. }
  126.  
  127. OSErr InstallMyCoercers( void )
  128. {
  129.     OSErr err;
  130.  
  131.     err = AEInstallCoercionHandler( typeChar,
  132.                                     typePString,
  133.                                     (ProcPtr)TextPtrToPString,
  134.                                     0L,
  135.                                     false,        /* Pass a pointer not a descriptor */
  136.                                     false );    /* Application table, not System */
  137.     if ( err ){
  138.         Gripe( "\pAEInstallCoercionHandler failed on TextPtrToPString" );
  139.         return err;
  140.     }
  141.  
  142.     return noErr;
  143. }
  144.  
  145. /* This is a single routine that must know how to count each of the kinds of objects
  146.  * that are used in this application.  Eventually we should have it call functions
  147.  * that are in the ObjXXX files.
  148.  */
  149.  
  150. pascal OSErr MyCountProc(DescType desiredType,
  151.                         DescType containerClass,
  152.                         const AEDesc *container,
  153.                         long *result)
  154. {
  155.     OSErr err = noErr;
  156.     
  157.     switch ( desiredType ){
  158.         case cWindow:
  159.             *result = ( gDocWindow ? 1 : 0 );
  160.             break;
  161.         default:
  162.             Gripe( "\pTrying to count an object I do not know about" );
  163.             err = errAENoSuchObject;
  164.             break;
  165.     }
  166.     
  167.     return err;
  168. }
  169.  
  170. pascal OSErr MyDisposeTokenProc(AEDesc *unneededToken)
  171. {
  172.     OSErr myErr = noErr;
  173.  
  174.     /* In most cases we just toss out the token descriptor.  We may need to do more
  175.      * stuff in the event the the token handle actually points to some real data.
  176.      */
  177.  
  178.     switch (unneededToken->descriptorType) {
  179.         case cWindow:
  180.             myErr = AEDisposeDesc(unneededToken);
  181.             break;
  182.  
  183.         default:
  184.             /* I default to just disposing of the token, ne ces pa  */
  185.             AEDisposeDesc(unneededToken);
  186.             break;
  187.     }
  188.     return(myErr);
  189. }
  190.  
  191. OSErr MakeTextDesc( AEDesc *descPtr )
  192. {
  193.     
  194.     return noErr;
  195. }